package com.example.gcmtest;

import com.google.android.maps.GeoPoint;

public class CalcPos{

	/* コンストラクタ */
	public CalcPos(){
	}

	/* 補正値算出用メソッド */
	public String resultPosition(GeoPoint gp_S, GeoPoint gp_E, GeoPoint gp_P){

		double xe;
		double xs;
		double ye;
		double ys;
		double yp;
		double xp;
		double a;
		double b;
		double c;
		double L;
		double ex;
		double ey;
		double vx;
		double vy;
		double k;
		double xq;
		double yq;
		String latlonString;

		xe = gp_E.getLatitudeE6() / 1e6;
		xs = gp_S.getLatitudeE6() / 1e6;
		ye = gp_E.getLongitudeE6() / 1e6;
		ys = gp_S.getLongitudeE6() / 1e6;
		yp = gp_P.getLongitudeE6() / 1e6;
		xp = gp_P.getLatitudeE6() / 1e6;

		a = ye - ys;
		b = xs - xe;
		c = -((a * xs) + (b * ys));
		L = Math.sqrt(Math.pow(xe - xs, 2) + Math.pow(ye - ys, 2));
		ex = (xe - xs) / L;
		ey = (ye - ys) / L;
		vx = -ey;
		vy = ex;
		k = -((a * xp) + (b * yp) + c)/((a * vx) + (b * vy));

		xq = xp + k * vx;
		yq = yp + k * vy;

		latlonString = Double.toString(xq) + "," +  Double.toString(yq) + ",";

//		Log.v("calcpos","xq :" + xq );
//		Log.v("calcpos","yq :" + yq );
//		Log.v("calcpos","latlonString :" + latlonString );

		return latlonString;
	}
}

